<?php
// Version: 1.5; Groupmods_install

// Create the group_moderators table, copying from groupmods if it exists
db_query("
	CREATE TABLE IF NOT EXISTS {$db_prefix}groupmods (
	`ID_GROUP` smallint(5) unsigned NOT NULL default '0',
	`ID_MEMBER` mediumint(8) unsigned NOT NULL default '0',
	PRIMARY KEY ( `ID_GROUP` , `ID_MEMBER` )
	)", __FILE__, __LINE__);
db_query("
	CREATE TABLE IF NOT EXISTS {$db_prefix}group_moderators (
	`id_group` smallint(5) unsigned NOT NULL default '0',
	`id_member` mediumint(8) unsigned NOT NULL default '0',
	PRIMARY KEY (`id_group`, `id_member`)
	)", __FILE__, __LINE__);
db_query("
	INSERT IGNORE INTO {$db_prefix}group_moderators (id_group, id_member)
	SELECT ID_GROUP, ID_MEMBER
	FROM {$db_prefix}groupmods", __FILE__, __LINE__);
db_query("
	DROP TABLE {$db_prefix}groupmods", __FILE__, __LINE__);

// Add the GroupModOptions field to the membergroups table
$request = db_query("
	SHOW COLUMNS
	FROM {$db_prefix}membergroups
	LIKE 'GroupModOptions'", __FILE__, __LINE__);
$no_upgrade = mysql_num_rows($request) > 0;
if (!$no_upgrade) db_query("
	ALTER TABLE {$db_prefix}membergroups
	ADD `GroupModOptions` VARCHAR( 8 ) NOT NULL", __FILE__, __LINE__);
mysql_free_result($request);

// Add default permissions
$groups = array(); 
// Get all the non-postcount based groups.
// guests(-1) and 'ungrouped members' (0) should not see the groups
// administrators (1) do not have permissions .. they can see everything
$request = db_query("
	SELECT ID_GROUP
	FROM {$db_prefix}membergroups
	WHERE minPosts = -1 AND ID_GROUP > 1", __FILE__, __LINE__); 
while ($row = mysql_fetch_assoc($request))
	$groups[] = $row['ID_GROUP'];
// Give them all their new permission.
$request = db_query("
	INSERT IGNORE INTO {$db_prefix}permissions
		(permission, ID_GROUP, addDeny)
	VALUES
		('view_groups', " . implode(", 1),
		('view_groups', ", $groups) . ", 1)", __FILE__, __LINE__);

?>
